package com.eightbears.beboy.ui.chat.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.eightbears.beboy.R;
import com.eightbears.beboy.base.BaseActivity;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.view.SwipeBackLayout;
import com.eightbears.beboy.entity.UserEntity;
import com.eightbears.beboy.ui.chat.ChatUtil;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.exceptions.HyphenateException;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.eightbears.beboy.base.util.CommonAPI.GROUP_EXCHANGE_GROUP_NOTICE;
import static com.eightbears.beboy.base.util.CommonAPI.PARAMS_GROUP_ID;
import static com.eightbears.beboy.base.util.CommonAPI.PARAMS_GROUP_NOTICE;


public class NoticeEditActivity extends BaseActivity {

    public static final String GROUP_DIR_UPDATE = "dirUpdate";

    @BindView(R.id.tv_submit)
    TextView tv_submit;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.et_content)
    EditText et_content;
    @BindView(R.id.tv_notice)
    TextView tv_notice;
    @BindView(R.id.sw_finish)
    SwipeBackLayout sw_finish;

    private UserEntity mUserEntity;

    private String groupId;
    private boolean isSelf = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice_edit);
        initView();
        initData();
        initEvent();
    }


    @Override
    protected void initView() {
        ButterKnife.bind(this);

        tv_title.setText("群公告");
    }

    @Override
    protected void initData() {
        groupId = getIntent().getStringExtra(PARAMS_GROUP_ID);
        EMGroup group = EMClient.getInstance().groupManager().getGroup(groupId);
        mUserEntity = SPUtil.getUser();

        String content = group.getDescription();

        if (group.getOwner() == null || "".equals(group.getOwner())
                || !group.getOwner().equals(EMClient.getInstance().getCurrentUser())) {
            et_content.setVisibility(View.GONE);
            tv_submit.setVisibility(View.GONE);
            tv_notice.setVisibility(View.VISIBLE);
            if (null != content && !content.trim().equals("")) {
                tv_notice.setText(content);
            } else {
                tv_notice.setText("暂无公告");
            }
        } else {
            et_content.setText(content.trim());
            et_content.setVisibility(View.VISIBLE);
            tv_submit.setVisibility(View.VISIBLE);
            tv_notice.setVisibility(View.GONE);
        }


    }

    @Override
    protected void initEvent() {
        sw_finish.setCallBack(new SwipeBackLayout.CallBack() {
            @Override
            public void onFinish() {
                finish();
            }
        });
    }

    @OnClick(R.id.tv_submit)
    void submit() {
        final String notice = et_content.getText().toString().trim();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    EMClient.getInstance().groupManager().changeGroupDescription(groupId, notice);
                    ChatUtil.sendCMSMessage(GROUP_EXCHANGE_GROUP_NOTICE, PARAMS_GROUP_NOTICE, notice, groupId, mUserEntity.getData().getUser_id()+"");
                    EventBus.getDefault().post(GROUP_DIR_UPDATE);
                    finish();
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @OnClick(R.id.ll_back)
    void back() {
        finish();
    }
}
